package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.Attachment;
import cn.ztydata.teachingsystem.heaven.entity.Resource;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 资源详细Dao
 *
 * @author fanruiping
 * @since 2014-12-09
 */
@Repository
public class ResourceInfoDao extends BaseDao {
    /**
     * 新增资源
     * @param entity
     *
     * @author fanruiping
     * @since 2014-12-09
     */
    public boolean add(Resource entity){
        String sql = "INSERT INTO t_resource_info(iResourceId,sOrigin,sContent,sRemark) VALUES(" +
                ":id,:origin,:content,:remark)";

        MapSqlParameterSource params = new MapSqlParameterSource().addValue("id", entity.getId()).addValue("origin",entity.getOrigin())
                .addValue("content", entity.getContent()).addValue("remark",entity.getRemark());

        log.debug("新增资源详细信息");

        return namedParameterJdbcTemplate.update(sql, params) == 1;
    }

    /**
     * 编辑资源
     * @param entity
     * @return boolean
     *
     * @author fanruiping
     * @since 2014-12-24
     */
    public boolean modify(Attachment entity){
        String sql = "UPDATE t_resource_info SET sShowAddr=:showAddr WHERE iResourceId=:id";

        MapSqlParameterSource params = new MapSqlParameterSource().addValue("id", entity.getId())
                .addValue("showAddr", entity.getDownloadUrl());

        log.debug("update resource attachment info");

        return namedParameterJdbcTemplate.update(sql, params) == 1;
    }

    /**
     * 资源明细
     * @param id
     * @return Resource
     *
     * @author fanruiping
     * @since 2014-12-09
     */
    public Resource getById(long id){
        try{
            String sql = "SELECT iResourceId,sOrigin,sShowAddr,sContent,sRemark FROM t_resource_info WHERE iResourceId=:id";

            log.debug("return resource info by id");

            return namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource().addValue("id",id),CreateMapper());
        }catch (EmptyResultDataAccessException ex){
            log.debug("catch EmptyResultDataAccessException:return null");

            return null;
        }
    }

    /**
     * 删除指定资源详细
     * @param id 资源编号
     * @return int
     *
     * @author fanruiping
     * @since 2014-12-10
     */
    public int delete(long id){
        String sql = "DELETE FROM t_resource_info WHERE iResourceId=:id";

        log.debug("delete resource info by resourceId");

        return namedParameterJdbcTemplate.update(sql,new MapSqlParameterSource().addValue("id", id));
    }

    /**
     * ResultSet转为Resource实体
     * @return RowMapper
     *
     * @author fanruiping
     * @since 2014-12-09
     */
    private RowMapper<Resource> CreateMapper(){
        return new RowMapper<Resource>() {
            @Override
            public Resource mapRow(ResultSet rs, int i) throws SQLException {
            Resource entity = new Resource();

            entity.setId(rs.getLong("iResourceId"));
            entity.setOrigin(rs.getString("sOrigin"));
            entity.setShowAddr(rs.getString("sShowAddr"));
            entity.setContent(rs.getString("sContent"));
            entity.setRemark(rs.getString("sRemark"));

            return entity;
            }
        };
    }
}
